/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.longview.io.modules.base.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.longview.io.common.config.Global;
import com.longview.io.common.persistence.Page;
import com.longview.io.common.web.BaseController;
import com.longview.io.common.utils.StringUtils;
import com.longview.io.modules.base.entity.Station;
import com.longview.io.modules.base.service.StationService;

/**
 * 站点基础信息表Controller
 * @author zf
 * @version 2017-09-18
 */
@Controller
@RequestMapping(value = "${adminPath}/base/station")
public class StationController extends BaseController {

	@Autowired
	private StationService stationService;
	
	@ModelAttribute
	public Station get(@RequestParam(required=false) String id) {
		Station entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = stationService.get(id);
		}
		if (entity == null){
			entity = new Station();
		}
		return entity;
	}
	
	@RequiresPermissions("base:station:view")
	@RequestMapping(value = {"list", ""})
	public String list(Station station, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Station> page = stationService.findPage(new Page<Station>(request, response), station); 
		model.addAttribute("page", page);
		return "modules/base/stationList";
	}

	@RequiresPermissions("base:station:view")
	@RequestMapping(value = "form")
	public String form(Station station, Model model) {
		model.addAttribute("station", station);
		return "modules/base/stationForm";
	}

	@RequiresPermissions("base:station:edit")
	@RequestMapping(value = "save")
	public String save(Station station, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, station)){
			return form(station, model);
		}
		stationService.save(station);
		addMessage(redirectAttributes, "保存站点基础信息表成功");
		return "redirect:"+Global.getAdminPath()+"/base/station/?repage";
	}
	
	@RequiresPermissions("base:station:edit")
	@RequestMapping(value = "delete")
	public String delete(Station station, RedirectAttributes redirectAttributes) {
		stationService.delete(station);
		addMessage(redirectAttributes, "删除站点基础信息表成功");
		return "redirect:"+Global.getAdminPath()+"/base/station/?repage";
	}
	
	/**
	 * 借助中间表同步站点area_id
	 */
	@RequiresPermissions("base:station:edit")
	@RequestMapping(value = "updateCityId")
	public void updateCityId(Station station, RedirectAttributes redirectAttributes) {
		stationService.updateCityId(station);
	}

	/***
	 * 选择站点
	 * @param station
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = {"changeStation"})
	public String changeStation(Station station, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Station> page = stationService.findPage(new Page<Station>(request, response), station); 
		model.addAttribute("page", page);
		return "modules/base/changeStationList";
	}
}